//
//  DTKImagePicker.m
//  DTKCheckBundle
//
//  Created by mac on 2024/7/25.
//

#import "DTKImagePicker.h"
#import <UIKit/UIKit.h>
#import <TZImagePickerController/TZImagePickerController.h>
#import "DTKMapBundle.h"
static DTKImagePicker *picker = nil;

@interface DTKImagePicker()<UIImagePickerControllerDelegate, UINavigationControllerDelegate, TZImagePickerControllerDelegate>

@property(nonatomic,copy)DTKExternalAPICallback _Nullable callBack;

@end

@implementation DTKImagePicker
+ (instancetype)sharedInstance {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        picker = [[DTKImagePicker alloc]init];
    });
    return picker;
}

-(void)takePhoto:(DTKExternalAPICallback)callBack {
    UIImagePickerController *picker = [[UIImagePickerController alloc]init];
    picker.sourceType = UIImagePickerControllerSourceTypeCamera;
    picker.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;
    picker.delegate = self;
    picker.modalPresentationStyle = UIModalPresentationFullScreen;
    [[[[DTKMapBundle services]uiHierachyService]currentTopViewController] presentViewController:picker animated:YES completion:nil];
    self.callBack = callBack;
    return;
}

-(void)pickPhotos:(NSInteger)count completion:(DTKExternalAPICallback)callBack {
    TZImagePickerController *picker = [[TZImagePickerController alloc]initWithMaxImagesCount:count delegate:self];
    picker.modalPresentationStyle = UIModalPresentationFullScreen;
    picker.allowTakeVideo = NO;
    picker.allowTakePicture = NO;
    picker.allowPickingGif = NO;
    picker.allowPickingVideo = NO;
    picker.allowPickingMultipleVideo = NO;
    picker.didFinishPickingPhotosHandle = ^(NSArray *photos, NSArray *assets, BOOL origin) {
        if (callBack) {
            callBack(photos);
        }
    };
    picker.imagePickerControllerDidCancelHandle = ^{
        if (callBack) {
            NSError *error = [NSError errorWithDomain:NSCocoaErrorDomain code:-1 userInfo:nil];
            callBack(error);
        }
    };
    [[[[DTKMapBundle services]uiHierachyService]currentTopViewController] presentViewController:picker animated:YES completion:nil];
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<UIImagePickerControllerInfoKey,id> *)info {
    UIImage *selectedImage = info[UIImagePickerControllerOriginalImage];
    if (selectedImage) {
        if (self.callBack) {
            self.callBack(@[selectedImage]);
        }
    } else {
        if (self.callBack) {
            NSError *error = [NSError errorWithDomain:NSCocoaErrorDomain code:-1 userInfo:nil];
            self.callBack(error);
        }
    }
    self.callBack = nil;
    [picker dismissViewControllerAnimated:YES completion:nil];
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    if (self.callBack) {
        NSError *error = [NSError errorWithDomain:NSCocoaErrorDomain code:-1 userInfo:nil];
        self.callBack(error);
    }
    self.callBack = nil;
    [picker dismissViewControllerAnimated:YES completion:nil];
}

@end
